package com.neuedu.His02.controller.outpatientDoctorWorkStation;

import com.neuedu.His02.entity.CheckApply;
import com.neuedu.His02.entity.MedicalRecord;
import com.neuedu.His02.entity.Register;
import com.neuedu.His02.entity.User;
import com.neuedu.His02.service.outpatientDoctorWorkStationService.DisposalApplicationService;
import com.neuedu.His02.util.EnumCode;
import com.neuedu.His02.util.ResultUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 处置申请控制类
 */
@RestController
public class DisposalApplicationController {

    @Resource
    private DisposalApplicationService service;

    /**
     * @return {@link ResultUtil}
     * @description 初始化待申请处置的患者
     */
    @PostMapping("/outpatientDoctorWorkStation/disposalApplicationService/initPatients")
    public ResultUtil initPatients(@RequestBody User user) {
        System.out.println("初始化待申请患者:"+user);
//        根据医生来找病历表中符合要求的数据
        List<Register> caseNumber=new ArrayList<>();
        caseNumber=service.initPatients(user);
        //在从上面数据里在患者病历首页表里查找CaseState=2的数据
        class Temp extends MedicalRecord{
            private String RealName; //姓名
            private Integer Age; //年龄

            public String getRealName() {
                return RealName;
            }

            public void setRealName(String realName) {
                RealName = realName;
            }
            public Integer getAge() {
                return Age;
            }

            public void setAge(Integer age) {
                Age = age;
            }
            public Temp(){

            }
            public Temp(String name,Integer age){
                RealName=name;
                Age=age;
            }
        }
        List<Temp> temp=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="初始化成功";
        try {
            for (int i = 0; i <caseNumber.size() ; i++) {
                System.out.println("caseNumber"+"["+i+"]"+caseNumber.get(i).getCaseNumber()+caseNumber.get(i).getRealName()+
                        caseNumber.get(i).getAge());
                // 查询每一个记录是否在患者病历首页表里，是就添加到新的类中，否就跳过
                if (service.initRecords(caseNumber.get(i).getCaseNumber())!=null){
                    Temp t=new Temp();
                    t.setRealName(caseNumber.get(i).getRealName());
                    t.setAge(caseNumber.get(i).getAge());
                    t.setCaseNumber(caseNumber.get(i).getCaseNumber());
                    t.setID(service.initRecords(caseNumber.get(i).getCaseNumber()).getID());
                    t.setRegistID(service.initRecords(caseNumber.get(i).getCaseNumber()).getRegistID());
                    t.setReadme(service.initRecords(caseNumber.get(i).getCaseNumber()).getReadme());
                    t.setDiagnosis(service.initRecords(caseNumber.get(i).getCaseNumber()).getDiagnosis());
                    t.setPresent(service.initRecords(caseNumber.get(i).getCaseNumber()).getPresent());
                    t.setPhysique(service.initRecords(caseNumber.get(i).getCaseNumber()).getPhysique());
                    t.setPresentTreat(service.initRecords(caseNumber.get(i).getCaseNumber()).getPresentTreat());
                    t.setProposal(service.initRecords(caseNumber.get(i).getCaseNumber()).getProposal());
                    t.setAllergy(service.initRecords(caseNumber.get(i).getCaseNumber()).getAllergy());
                    t.setCareful(service.initRecords(caseNumber.get(i).getCaseNumber()).getCareful());
                    t.setCaseState(service.initRecords(caseNumber.get(i).getCaseNumber()).getCaseState());
                    t.setCheckResult(service.initRecords(caseNumber.get(i).getCaseNumber()).getCheckResult());
                    t.setHandling(service.initRecords(caseNumber.get(i).getCaseNumber()).getHandling());
                    System.out.println("符合要求的数据:"+i);
                    temp.add(t);
                }
            }
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="初始化失败";
        }
        System.out.println("即将返回的数据:"+temp);
        return new ResultUtil(code,msg,temp);
    }

    /**
     * @return {@link ResultUtil}
     * @description 初始化患者的处置申请信息
     */
    @RequestMapping("/outpatientDoctorWorkStation/disposalApplicationService/initApply")
    public ResultUtil initApply(MedicalRecord medicalRecord) {
        System.out.println("初始化患者的处置申请"+medicalRecord);
        List<CheckApply> result=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="初始化成功";
        boolean flag=true;
        try {
            if (service.applyIsExist(medicalRecord)==null) {
                flag=false;
                System.out.println("此患者暂无处置信息");
            }else {
                result=service.getApply(medicalRecord);

            }
            }catch (Exception e){
                code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
                msg="初始化失败";
        }
        if(flag){
            return new ResultUtil(code,msg,result);
        }else {
            return new ResultUtil(code,msg);
        }
    }

    /**
     * @return {@link ResultUtil}
     * @description 暂存患者的处置项目
     */
    @PostMapping("/outpatientDoctorWorkStation/disposalApplicationService/tempApply")
    public ResultUtil tempApply(@RequestBody CheckApply checkApply) {
        System.out.println("即将暂存的项目:"+checkApply);
        Integer code=EnumCode.OK.getValue();
        String msg="暂存成功";
        try {
            //先在数据库里查找是否有这个项目
            if(service.checkApplyIsExist(checkApply)!=null){
                //没有则插入一个记录
                service.insertCheckApply(checkApply);
            }else {
                //否则更新
                service.updateCheckApply(checkApply);
            }
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="暂存失败";
        }
        return new ResultUtil(code,msg);
    }

    /**
     * @return {@link ResultUtil}
     * @description 开立患者的处置项目
     */
    @PostMapping("/outpatientDoctorWorkStation/disposalApplicationService/commitApply")
    public ResultUtil commitApply(@RequestBody CheckApply checkApply) {
        System.out.println("即将开立的项目:"+checkApply);
        Integer code=EnumCode.OK.getValue();
        String msg="开立成功";
        try {
            service.updateCheckApply(checkApply);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="开立失败";
        }
        return new ResultUtil(code,msg);
    }

    /**
     * @return {@link ResultUtil}
     * @description 删除患者的处置项目
     */
    @PostMapping("/outpatientDoctorWorkStation/disposalApplicationService/deleteApply")
    public ResultUtil deleteApply(@RequestBody CheckApply checkApply) {
        System.out.println("即将要删除的项目:"+checkApply);
        Integer code=EnumCode.OK.getValue();
        String msg="删除成功";
        try {
            service.deleteCheckApply(checkApply);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="删除失败";
        }
        return new ResultUtil(code,msg);
    }

    /**
     * @return {@link ResultUtil}
     * @description 作废患者的处置项目
     */
    @PostMapping("/outpatientDoctorWorkStation/disposalApplicationService/cancelApply")
    public ResultUtil cancelApply(@RequestBody CheckApply checkApply) {
        System.out.println("即将作废的项目:"+checkApply);
        Integer code=EnumCode.OK.getValue();
        String msg="作废成功";
        try {
            service.updateCheckApply(checkApply);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="作废失败";
        }
        return new ResultUtil(code,msg);
    }

    /**
     * @return {@link ResultUtil}
     * @description 修改患者的处置项目
     */
    @PostMapping("/outpatientDoctorWorkStation/disposalApplicationService/editApply")
    public ResultUtil editApply(@RequestBody CheckApply checkApply) {
        System.out.println("即将要修改的项目:"+checkApply);
        Integer code=EnumCode.OK.getValue();
        String msg="修改成功";
        try {
            service.updateCheckApply(checkApply);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="修改失败";
        }
        return new ResultUtil(code,msg);
    }

    /**
     * @return {@link ResultUtil}
     * @description 生成项目的名称
     */
    @PostMapping("/outpatientDoctorWorkStation/disposalApplicationService/initItemName")
    public ResultUtil initItemName() {
        List<?> result=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="生成成功";
        try {
            service.initItemName();
            System.out.println("即将返回的项目:"+result);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="生成失败";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 添加患者的处置项目
     */
    @PostMapping("/outpatientDoctorWorkStation/disposalApplicationService/addApply")
    public ResultUtil addApply(@RequestBody CheckApply checkApply) {
        System.out.println("即将添加的项目:"+checkApply);
        Integer code=EnumCode.OK.getValue();
        String msg="添加成功";
        try {
            service.addApply(checkApply);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="添加成功";
        }
        return new ResultUtil(code,msg);
    }
}
